/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "los_hwi.h"
#include "los_task.h"
#include "los_api_bsp_gmc.h"
#include "los_inspect_entry.h"
#include "csg_sdk_hal.h"
#include "lwip/init.h"
#include "lwip/tcpip.h"
#include "netif/etharp.h"
#include "ethernetif.h"
#include "lwip/tcpip.h"
#include "pcram.h"
#include "lwip/sockets.h"
#include "socket_lwip_test.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

#define MSG "UDP sendto success!"

static char g_buf[BUF_SIZE + 1] = { 0 };
static char g_buf_temp[BUF_SIZE + 1] = { 0 };
static int g_testError = 0;

void Example_Lwip_UDP_sendto_Success(void)
{

    int sfd;
    struct sockaddr_in srvAddr = { 0 };
    struct sockaddr_in clnAddr = { 0 };
    socklen_t clnAddrLen = sizeof(clnAddr);
    int ret;
    struct msghdr msg = { 0 };
    struct iovec iov[IOV_LENGTH] = { 0 };
    int recvCount = 0;
    int i = 0;
    dprintf("\r\n--------4.31 Lwip UDP sendto success test start!--------\r\n");
    /* socket creation */
    sfd = socket(AF_INET, SOCK_DGRAM, 0);
    LWIP_ASSERT("socket invalid param.", sfd != -1);
    dprintf("udp create socket ret=%d\r\n", ret);

    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr(LOCALHOST);
    srvAddr.sin_port = htons(LOCAL_PORT);
    ret = bind(sfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
    LWIP_ASSERT("socket invalid param.", ret == 0);

    /* send */
    clnAddr.sin_family = AF_INET;
    clnAddr.sin_addr.s_addr = inet_addr(REMOTEHOST);
    clnAddr.sin_port = htons(REMOTE_PORT);
    (void)memset(g_buf, 0, sizeof(g_buf));
    (void)strncpy(g_buf, MSG, sizeof(MSG));
    for(i=0;i<10;i++)
    {
        ret = sendto(sfd, g_buf, strlen(MSG), 0, (struct sockaddr*)&clnAddr,
            (socklen_t)sizeof(clnAddr));
        LWIP_ASSERT("socket invalid param.", ret != -1);    
        dprintf("udp send msg-%s len=%d\r\n", MSG, ret);
        LOS_TaskDelay(1);
    }


    LOS_TaskDelay(2000);

    /* close socket */
    ret = closesocket(sfd);
    LWIP_ASSERT("socket invalid param.", ret != -1);
    dprintf("udp close socket ret=%d\r\n", ret);

    dprintf("net_socket_udp_send_test exit\r\n");
    return;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
